﻿using Microsoft.EntityFrameworkCore;
using StockTrading.Model;
using StockTrading.View.Pages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace StockTrading.ViewModel
{
   public class StockViewModel:NotificationBase
    {
        private readonly ApplicationDbContext context;
        public List<Stock> Stocks { get; set; }

        private Stock _SelectedItem;
        public StockViewModel(MainViewModel mainViewModel,StockDetails stockDetails, ApplicationDbContext _context)
        {
            MainViewModel = mainViewModel;
            StockDetails = stockDetails;
            context = _context;
            this.Stocks = _context.Stocks.ToList();

            this.ChangeToStockDetail = new CommandBase();
            this.ChangeToStockDetail.ExecuteAction = ToStockDetail;
            this.ChangeToStockDetail.CanExecuteFunc = p =>
            {
                if (p == null)
                    return true;
                else
                    return false;
            };
        }
        public Stock SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                this._SelectedItem = value;
                this.RaisePropertyChange("SelectedItem");
            }
        }

        public CommandBase ChangeToStockDetail { set; get; }
        public MainViewModel MainViewModel { get; }
        public StockDetails StockDetails { get; }


        public async void ToStockDetail(object param)
        {
            this.MainViewModel.CurrentPage = StockDetails;
            this.StockDetails.StockDetailsViewModel.CurrentStock = this.SelectedItem;
            this.StockDetails.StockDetailsViewModel.LoginUser.CurUserPosition = await context.UserPositions.FirstOrDefaultAsync(x => this.StockDetails.StockDetailsViewModel.LoginUser.UserModel.Id == x.UserId && x.StockId == this.SelectedItem.Id);
            var order = await context.Orders.Where(x =>  x.StockId == SelectedItem.Id).OrderByDescending(x => x.CreateDate).FirstOrDefaultAsync();
            this.StockDetails.StockDetailsViewModel.StockInfo.Price = order?.Price ?? 0;
            this.StockDetails.StockDetailsViewModel.StockInfo.Avg = Math.Round(await context.Orders.AverageAsync(x => x.Price),2);
            this.StockDetails.StockDetailsViewModel.StockInfo.TranSum = Math.Round(await context.Transactions.Include(x => x.Sell_Order).Include(y => y.Sell_Order).SumAsync(x => x.Buy_Order.Price * x.Buy_Order.Dealed), 2);
            var today = await context.Orders.Where(x => x.CreateDate.Date.CompareTo(System.DateTime.Now.Date) == 0).OrderBy(x => x.CreateDate).FirstOrDefaultAsync();
            var yestoday = await context.Orders.Where(x => x.CreateDate.Date.CompareTo(System.DateTime.Now.Date.AddDays(-1)) == 0).OrderByDescending(x => x.CreateDate).FirstOrDefaultAsync();

            this.StockDetails.StockDetailsViewModel.StockInfo.Today = Math.Round(today?.Price ?? 0);
            this.StockDetails.StockDetailsViewModel.StockInfo.YesToday = Math.Round(yestoday?.Price ?? 0);

        }

    }
}
